import java.util.ArrayList;
import java.io.*;

/** 
 * 
 * @author CJ Buice
 * @version 2011.0426
 * @since 1.6
 *
 */
public class ASBApplicationProcessor {
	// TRIPMAX defines the maximum number of people that are allowed per trip.
	private static final int TRIPMAX = 2;
	// TRIPCOUNT defines the total number of trips.
	private static final int TRIPCOUNT = 10;
	// tripNames contains the names of the trips for comparison with those from the applications.
	private static final String[] tripNames = {"georgia", "alabama", "florida", "washington", "new york", 
		"arkansas", "south carolina", "california", "mississippi", "arizona"};
	// trips holds the lists of people on each trip.
	private static ArrayList<ArrayList<String>> trips = new ArrayList<ArrayList<String>>(0);
	// applicants holds the applicant data imported from the .csv file.
	private static ArrayList<String[]> applicants = new ArrayList<String[]>(0);
	// waitlist holds the list of people on the waiting list.
	private static ArrayList<String[]> waitlist = new ArrayList<String[]>(0);
	
	public ASBApplicationProcessor() {

	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			initializeTrips();
			ParseCSV parser = new ParseCSV(applicants);
			parser.processLineByLine();
			assignTrips();
			output();
		}
		catch (FileNotFoundException e) {
			System.err.println("FileNotFoundException: " + e.getMessage());
		}
		catch (IOException e) {
			System.err.println("IOException: " + e.getMessage());
		}
		finally {	
		}
	}

	/**
	 * Initializes the ArrayList containers for the trip lists.
	 */
	private static void initializeTrips() {
		ArrayList<String> trip1 = new ArrayList<String>(0);
		ArrayList<String> trip2 = new ArrayList<String>(0);
		ArrayList<String> trip3 = new ArrayList<String>(0);
		ArrayList<String> trip4 = new ArrayList<String>(0);
		ArrayList<String> trip5 = new ArrayList<String>(0);
		ArrayList<String> trip6 = new ArrayList<String>(0);
		ArrayList<String> trip7 = new ArrayList<String>(0);
		ArrayList<String> trip8 = new ArrayList<String>(0);
		ArrayList<String> trip9 = new ArrayList<String>(0);
		ArrayList<String> trip10 = new ArrayList<String>(0);
		trips.add(trip1);
		trips.add(trip2);
		trips.add(trip3);
		trips.add(trip4);
		trips.add(trip5);
		trips.add(trip6);
		trips.add(trip7);
		trips.add(trip8);
		trips.add(trip9);
		trips.add(trip10);
	}
	
	/**
	 * Processes the input application content and assigns each
	 */
	private static void assignTrips() {
		for (int i = 0; i < applicants.size(); i++) {
			boolean added = false;
			for (int j = 1; j < applicants.get(i).length; j++) {
				for (int k = 0; k < TRIPCOUNT; k++) {
					if ((applicants.get(i)[j].equals(tripNames[k])) && (trips.get(k).size() < TRIPMAX)) {
						trips.get(k).add(applicants.get(i)[0]);
						added = true;
					}
					if (added) break;
				}
				if (added) break;
			}
			if (!added) waitlist.add(applicants.get(i));
		}
	}
	
	/**
	 * Outputs the content of the various ArrayLists into their respective .csv files.
	 * 
	 * @throws IOException
	 */
	private static void output() throws IOException {
		PrintWriter appsOut = new PrintWriter(new FileWriter("applicants.csv")); 
		for (int i = 0; i < applicants.size(); i++) {
			for (int j = 0; j < applicants.get(i).length; j++) {
				if (j == (applicants.get(i).length - 1)) appsOut.print(applicants.get(i)[j] + "\n");
				else appsOut.print(applicants.get(i)[j] + ", ");
			}
		}
		appsOut.close();
		for (int i = 0; i < tripNames.length; i++) {
			PrintWriter tripsOut = new PrintWriter(new FileWriter(tripNames[i] + "_Trip.csv")); 
			for (int j = 0; j < trips.get(i).size(); j++) {
				tripsOut.print(trips.get(i).get(j) + "\n");
			}
			tripsOut.close();
		}
		PrintWriter waitOut = new PrintWriter(new FileWriter("waiting_list.csv")); 
		for (int i = 0; i < waitlist.size(); i++) {
			for (int j = 0; j < waitlist.get(i).length; j++) {
				if (j == (waitlist.get(i).length - 1)) waitOut.print(waitlist.get(i)[j] + "\n");
				else waitOut.print(waitlist.get(i)[j] + ", ");
			}
		}
		waitOut.close(); 
	}
}
